<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Bean 拷贝之 MapStruct | 鱼皮的编程宝典</title>
    <meta name="generator" content="VuePress 1.9.10">
    <link rel="icon" href="/favicon.ico">
    <script>
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?2675818a983a3131404cee835018f016";
          var s = document.getElementsByTagName("script")[0]; 
          s.parentNode.insertBefore(hm, s);
        })();
      </script>
    <meta name="description" content="贴心的编程学习路线，全面的编程知识百科">
    <meta property="article:modified_time" content="2023-11-11T12:17:47.000Z">
    <meta property="og:site_name" content="鱼皮的编程宝典">
    <meta property="og:title" content="Bean 拷贝之 MapStruct">
    <meta property="og:type" content="article">
    <meta property="og:url" content="https://codefather.cn/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/">
    <meta name="twitter:title" content="Bean 拷贝之 MapStruct">
    <meta name="twitter:url" content="https://codefather.cn/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/">
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:label1" content="Written by">
    <meta name="twitter:label2" content="Filed under">
    <meta name="twitter:data2" content="程序员, 编程, 计算机">
    <meta property="article:tag" content="程序员">
    <meta name="keywords" content="程序员鱼皮, 编程学习路线, 编程知识百科, Java, 编程导航, 前端, 开发, 编程分享, 项目, IT, 求职, 面经">
    
    <link rel="preload" href="/assets/css/0.styles.40c15e22.css" as="style"><link rel="preload" href="/assets/js/app.12ab4756.js" as="script"><link rel="preload" href="/assets/js/2.fd06b53e.js" as="script"><link rel="preload" href="/assets/js/48.48f286ba.js" as="script"><link rel="prefetch" href="/assets/js/1.17ea1209.js"><link rel="prefetch" href="/assets/js/10.d9532d8f.js"><link rel="prefetch" href="/assets/js/100.064ea126.js"><link rel="prefetch" href="/assets/js/101.d64c5b33.js"><link rel="prefetch" href="/assets/js/102.5e29a62c.js"><link rel="prefetch" href="/assets/js/103.59ad6cd7.js"><link rel="prefetch" href="/assets/js/104.ae4763e8.js"><link rel="prefetch" href="/assets/js/105.2a9124be.js"><link rel="prefetch" href="/assets/js/106.529bca07.js"><link rel="prefetch" href="/assets/js/107.c8671a0d.js"><link rel="prefetch" href="/assets/js/108.55c93c64.js"><link rel="prefetch" href="/assets/js/109.7d1efa54.js"><link rel="prefetch" href="/assets/js/11.2f89b5eb.js"><link rel="prefetch" href="/assets/js/110.0caecbf5.js"><link rel="prefetch" href="/assets/js/111.a2aeae49.js"><link rel="prefetch" href="/assets/js/112.eb7ceda5.js"><link rel="prefetch" href="/assets/js/113.f304408d.js"><link rel="prefetch" href="/assets/js/114.988c637f.js"><link rel="prefetch" href="/assets/js/115.bfdeed31.js"><link rel="prefetch" href="/assets/js/116.a47b8c53.js"><link rel="prefetch" href="/assets/js/117.2e1e8250.js"><link rel="prefetch" href="/assets/js/118.758ce408.js"><link rel="prefetch" href="/assets/js/119.73d9e688.js"><link rel="prefetch" href="/assets/js/12.53aaa509.js"><link rel="prefetch" href="/assets/js/120.202a8b2e.js"><link rel="prefetch" href="/assets/js/121.ca91d59a.js"><link rel="prefetch" href="/assets/js/122.8804c0dd.js"><link rel="prefetch" href="/assets/js/123.ceada225.js"><link rel="prefetch" href="/assets/js/124.4b2718b7.js"><link rel="prefetch" href="/assets/js/125.72134235.js"><link rel="prefetch" href="/assets/js/126.167b162c.js"><link rel="prefetch" href="/assets/js/127.f8139c17.js"><link rel="prefetch" href="/assets/js/128.da64a377.js"><link rel="prefetch" href="/assets/js/129.c07963b1.js"><link rel="prefetch" href="/assets/js/13.52940164.js"><link rel="prefetch" href="/assets/js/130.f66fc8bb.js"><link rel="prefetch" href="/assets/js/131.a913c7dd.js"><link rel="prefetch" href="/assets/js/132.ea648654.js"><link rel="prefetch" href="/assets/js/133.074ca70e.js"><link rel="prefetch" href="/assets/js/134.dc79ddd8.js"><link rel="prefetch" href="/assets/js/135.c4516208.js"><link rel="prefetch" href="/assets/js/136.2546baf7.js"><link rel="prefetch" href="/assets/js/137.56454fc6.js"><link rel="prefetch" href="/assets/js/138.bdb002bf.js"><link rel="prefetch" href="/assets/js/139.40a2f9b5.js"><link rel="prefetch" href="/assets/js/14.ade1e74d.js"><link rel="prefetch" href="/assets/js/140.06b20427.js"><link rel="prefetch" href="/assets/js/141.8dd992e3.js"><link rel="prefetch" href="/assets/js/142.d9305485.js"><link rel="prefetch" href="/assets/js/143.41bf907e.js"><link rel="prefetch" href="/assets/js/144.c138960c.js"><link rel="prefetch" href="/assets/js/145.fc5b38c8.js"><link rel="prefetch" href="/assets/js/146.c9166b70.js"><link rel="prefetch" href="/assets/js/147.c31d8a01.js"><link rel="prefetch" href="/assets/js/148.5c0534ca.js"><link rel="prefetch" href="/assets/js/149.d2355ea2.js"><link rel="prefetch" href="/assets/js/15.970a8ca5.js"><link rel="prefetch" href="/assets/js/150.c1644898.js"><link rel="prefetch" href="/assets/js/151.b6e4adf0.js"><link rel="prefetch" href="/assets/js/152.7a5071aa.js"><link rel="prefetch" href="/assets/js/153.31abbc68.js"><link rel="prefetch" href="/assets/js/154.9c6bb079.js"><link rel="prefetch" href="/assets/js/155.e5506a40.js"><link rel="prefetch" href="/assets/js/156.992dab50.js"><link rel="prefetch" href="/assets/js/157.12a6ee84.js"><link rel="prefetch" href="/assets/js/158.84dc1045.js"><link rel="prefetch" href="/assets/js/159.fd2022b1.js"><link rel="prefetch" href="/assets/js/16.ff971500.js"><link rel="prefetch" href="/assets/js/160.8d8889cd.js"><link rel="prefetch" href="/assets/js/161.31797def.js"><link rel="prefetch" href="/assets/js/162.bfae7a75.js"><link rel="prefetch" href="/assets/js/163.e5e8094d.js"><link rel="prefetch" href="/assets/js/164.870f212c.js"><link rel="prefetch" href="/assets/js/165.8da20a31.js"><link rel="prefetch" href="/assets/js/166.950a187d.js"><link rel="prefetch" href="/assets/js/167.fd042c52.js"><link rel="prefetch" href="/assets/js/168.7d9f058d.js"><link rel="prefetch" href="/assets/js/169.205df80e.js"><link rel="prefetch" href="/assets/js/17.4b045b26.js"><link rel="prefetch" href="/assets/js/170.270108a3.js"><link rel="prefetch" href="/assets/js/171.9f03d27d.js"><link rel="prefetch" href="/assets/js/172.1882bac7.js"><link rel="prefetch" href="/assets/js/173.fc1fe7d4.js"><link rel="prefetch" href="/assets/js/174.d77c927b.js"><link rel="prefetch" href="/assets/js/175.019b9e83.js"><link rel="prefetch" href="/assets/js/176.5ce31bd8.js"><link rel="prefetch" href="/assets/js/177.9a2006f2.js"><link rel="prefetch" href="/assets/js/178.88265ac1.js"><link rel="prefetch" href="/assets/js/179.3205ce07.js"><link rel="prefetch" href="/assets/js/18.1cdd0514.js"><link rel="prefetch" href="/assets/js/180.2ff51f44.js"><link rel="prefetch" href="/assets/js/181.f792ce97.js"><link rel="prefetch" href="/assets/js/182.103328e1.js"><link rel="prefetch" href="/assets/js/183.40fde303.js"><link rel="prefetch" href="/assets/js/184.1f36ac78.js"><link rel="prefetch" href="/assets/js/185.cf3b1c00.js"><link rel="prefetch" href="/assets/js/186.f7981399.js"><link rel="prefetch" href="/assets/js/187.20fe296d.js"><link rel="prefetch" href="/assets/js/188.7b004fea.js"><link rel="prefetch" href="/assets/js/189.599ca2d9.js"><link rel="prefetch" href="/assets/js/19.5740e4d6.js"><link rel="prefetch" href="/assets/js/190.14087c07.js"><link rel="prefetch" href="/assets/js/191.b0f73f6d.js"><link rel="prefetch" href="/assets/js/192.9d171669.js"><link rel="prefetch" href="/assets/js/193.00975585.js"><link rel="prefetch" href="/assets/js/194.2c7c43b4.js"><link rel="prefetch" href="/assets/js/195.ff126254.js"><link rel="prefetch" href="/assets/js/196.6546ae1c.js"><link rel="prefetch" href="/assets/js/197.eedb67bb.js"><link rel="prefetch" href="/assets/js/198.acdcc22c.js"><link rel="prefetch" href="/assets/js/199.7993a6b2.js"><link rel="prefetch" href="/assets/js/20.8885a4fa.js"><link rel="prefetch" href="/assets/js/200.52f08765.js"><link rel="prefetch" href="/assets/js/201.b85ab10b.js"><link rel="prefetch" href="/assets/js/202.ccec1c85.js"><link rel="prefetch" href="/assets/js/203.3f50a6ed.js"><link rel="prefetch" href="/assets/js/204.f28b803b.js"><link rel="prefetch" href="/assets/js/205.c51c53fd.js"><link rel="prefetch" href="/assets/js/206.ba0aa8f6.js"><link rel="prefetch" href="/assets/js/207.0dce311a.js"><link rel="prefetch" href="/assets/js/208.0ea629a6.js"><link rel="prefetch" href="/assets/js/209.f48c3e71.js"><link rel="prefetch" href="/assets/js/21.0bb62db4.js"><link rel="prefetch" href="/assets/js/210.689db8f2.js"><link rel="prefetch" href="/assets/js/211.72bc0f1f.js"><link rel="prefetch" href="/assets/js/212.456135bd.js"><link rel="prefetch" href="/assets/js/213.d0ea50fc.js"><link rel="prefetch" href="/assets/js/214.1400da91.js"><link rel="prefetch" href="/assets/js/215.aeef3e5d.js"><link rel="prefetch" href="/assets/js/216.a60215af.js"><link rel="prefetch" href="/assets/js/217.d6cd8b69.js"><link rel="prefetch" href="/assets/js/218.1bd500fc.js"><link rel="prefetch" href="/assets/js/219.20faf611.js"><link rel="prefetch" href="/assets/js/22.3e8347a6.js"><link rel="prefetch" href="/assets/js/220.6714179b.js"><link rel="prefetch" href="/assets/js/221.fb5b643d.js"><link rel="prefetch" href="/assets/js/222.d96272e5.js"><link rel="prefetch" href="/assets/js/223.4cb7dc3d.js"><link rel="prefetch" href="/assets/js/224.500d94cc.js"><link rel="prefetch" href="/assets/js/225.d006c34e.js"><link rel="prefetch" href="/assets/js/226.73612150.js"><link rel="prefetch" href="/assets/js/227.b02db9d1.js"><link rel="prefetch" href="/assets/js/228.aad43ff2.js"><link rel="prefetch" href="/assets/js/229.65340802.js"><link rel="prefetch" href="/assets/js/23.ceb5a5ff.js"><link rel="prefetch" href="/assets/js/230.97cab104.js"><link rel="prefetch" href="/assets/js/231.8415349a.js"><link rel="prefetch" href="/assets/js/232.db6d2697.js"><link rel="prefetch" href="/assets/js/233.723d05b1.js"><link rel="prefetch" href="/assets/js/234.26ed5e94.js"><link rel="prefetch" href="/assets/js/235.a586122b.js"><link rel="prefetch" href="/assets/js/236.f802bda8.js"><link rel="prefetch" href="/assets/js/237.cc8767ad.js"><link rel="prefetch" href="/assets/js/238.6485459e.js"><link rel="prefetch" href="/assets/js/239.9fbf3a55.js"><link rel="prefetch" href="/assets/js/24.07267ac6.js"><link rel="prefetch" href="/assets/js/240.1188f244.js"><link rel="prefetch" href="/assets/js/241.cbfb154d.js"><link rel="prefetch" href="/assets/js/242.352cea5a.js"><link rel="prefetch" href="/assets/js/243.07947e1c.js"><link rel="prefetch" href="/assets/js/244.f5b26fa9.js"><link rel="prefetch" href="/assets/js/245.c6030e32.js"><link rel="prefetch" href="/assets/js/246.d3fe99f7.js"><link rel="prefetch" href="/assets/js/247.185ae95d.js"><link rel="prefetch" href="/assets/js/248.a5f1548c.js"><link rel="prefetch" href="/assets/js/249.19691113.js"><link rel="prefetch" href="/assets/js/25.15e1f641.js"><link rel="prefetch" href="/assets/js/250.72fa1d33.js"><link rel="prefetch" href="/assets/js/251.bedd56f7.js"><link rel="prefetch" href="/assets/js/252.32bbcc8b.js"><link rel="prefetch" href="/assets/js/253.b59e5846.js"><link rel="prefetch" href="/assets/js/254.c0e37e1a.js"><link rel="prefetch" href="/assets/js/255.850da3cc.js"><link rel="prefetch" href="/assets/js/256.952d9817.js"><link rel="prefetch" href="/assets/js/257.88667bb8.js"><link rel="prefetch" href="/assets/js/258.c5873c28.js"><link rel="prefetch" href="/assets/js/259.6ae1dcbc.js"><link rel="prefetch" href="/assets/js/26.35b2bbdc.js"><link rel="prefetch" href="/assets/js/260.cb940cc3.js"><link rel="prefetch" href="/assets/js/261.0686c3e9.js"><link rel="prefetch" href="/assets/js/262.d0719839.js"><link rel="prefetch" href="/assets/js/263.ae83ebc1.js"><link rel="prefetch" href="/assets/js/264.209ad8a3.js"><link rel="prefetch" href="/assets/js/265.be8d1ee8.js"><link rel="prefetch" href="/assets/js/266.c51bceaa.js"><link rel="prefetch" href="/assets/js/267.fe8af48a.js"><link rel="prefetch" href="/assets/js/268.e18ed861.js"><link rel="prefetch" href="/assets/js/269.7126031e.js"><link rel="prefetch" href="/assets/js/27.270fcb06.js"><link rel="prefetch" href="/assets/js/270.89ead106.js"><link rel="prefetch" href="/assets/js/271.0643d07a.js"><link rel="prefetch" href="/assets/js/272.32a8e4b0.js"><link rel="prefetch" href="/assets/js/273.9881da47.js"><link rel="prefetch" href="/assets/js/274.2de022ea.js"><link rel="prefetch" href="/assets/js/275.d9a4fe99.js"><link rel="prefetch" href="/assets/js/276.d33de2e9.js"><link rel="prefetch" href="/assets/js/277.1ec367ab.js"><link rel="prefetch" href="/assets/js/278.66ab7c11.js"><link rel="prefetch" href="/assets/js/279.ff51cd15.js"><link rel="prefetch" href="/assets/js/28.ef3f6db2.js"><link rel="prefetch" href="/assets/js/280.ef45ed5f.js"><link rel="prefetch" href="/assets/js/281.87f26b81.js"><link rel="prefetch" href="/assets/js/282.c8ef594b.js"><link rel="prefetch" href="/assets/js/283.15643091.js"><link rel="prefetch" href="/assets/js/284.c4f25b2e.js"><link rel="prefetch" href="/assets/js/285.2d16e4b6.js"><link rel="prefetch" href="/assets/js/286.44eba266.js"><link rel="prefetch" href="/assets/js/287.7613e62f.js"><link rel="prefetch" href="/assets/js/288.b9546d55.js"><link rel="prefetch" href="/assets/js/289.125f85d8.js"><link rel="prefetch" href="/assets/js/29.177558be.js"><link rel="prefetch" href="/assets/js/290.db375797.js"><link rel="prefetch" href="/assets/js/291.90946626.js"><link rel="prefetch" href="/assets/js/292.a4524678.js"><link rel="prefetch" href="/assets/js/293.3ed9b083.js"><link rel="prefetch" href="/assets/js/294.d6c14116.js"><link rel="prefetch" href="/assets/js/295.2b485032.js"><link rel="prefetch" href="/assets/js/296.29d9aa0f.js"><link rel="prefetch" href="/assets/js/297.bfd72435.js"><link rel="prefetch" href="/assets/js/298.fc056989.js"><link rel="prefetch" href="/assets/js/299.0a3f258e.js"><link rel="prefetch" href="/assets/js/3.4dc033c7.js"><link rel="prefetch" href="/assets/js/30.248a229d.js"><link rel="prefetch" href="/assets/js/300.d064d776.js"><link rel="prefetch" href="/assets/js/301.d95b9fc0.js"><link rel="prefetch" href="/assets/js/302.1afa637a.js"><link rel="prefetch" href="/assets/js/303.231beaa8.js"><link rel="prefetch" href="/assets/js/304.05df35d9.js"><link rel="prefetch" href="/assets/js/305.f2d83cf9.js"><link rel="prefetch" href="/assets/js/306.d54f5e5e.js"><link rel="prefetch" href="/assets/js/307.d5df9000.js"><link rel="prefetch" href="/assets/js/308.ef2f52b3.js"><link rel="prefetch" href="/assets/js/309.d1f13319.js"><link rel="prefetch" href="/assets/js/31.32a62b91.js"><link rel="prefetch" href="/assets/js/310.30b84be3.js"><link rel="prefetch" href="/assets/js/311.d79e3145.js"><link rel="prefetch" href="/assets/js/312.58e24f19.js"><link rel="prefetch" href="/assets/js/313.9809a122.js"><link rel="prefetch" href="/assets/js/314.e92e41b1.js"><link rel="prefetch" href="/assets/js/315.8c2fc1d4.js"><link rel="prefetch" href="/assets/js/316.a0904343.js"><link rel="prefetch" href="/assets/js/317.18e42b54.js"><link rel="prefetch" href="/assets/js/318.6b051d27.js"><link rel="prefetch" href="/assets/js/319.513b0197.js"><link rel="prefetch" href="/assets/js/32.23b75afc.js"><link rel="prefetch" href="/assets/js/320.ae91bc36.js"><link rel="prefetch" href="/assets/js/321.a194efea.js"><link rel="prefetch" href="/assets/js/322.8273020e.js"><link rel="prefetch" href="/assets/js/323.d332e08f.js"><link rel="prefetch" href="/assets/js/324.ed867b64.js"><link rel="prefetch" href="/assets/js/325.c43ccf9a.js"><link rel="prefetch" href="/assets/js/326.9497d879.js"><link rel="prefetch" href="/assets/js/327.7f095e40.js"><link rel="prefetch" href="/assets/js/328.cb5c5847.js"><link rel="prefetch" href="/assets/js/329.d36b59f3.js"><link rel="prefetch" href="/assets/js/33.b258b779.js"><link rel="prefetch" href="/assets/js/330.b1a092b9.js"><link rel="prefetch" href="/assets/js/331.ff86566f.js"><link rel="prefetch" href="/assets/js/332.0daafa9f.js"><link rel="prefetch" href="/assets/js/333.b7e50524.js"><link rel="prefetch" href="/assets/js/334.7dcf9f0b.js"><link rel="prefetch" href="/assets/js/335.862c410a.js"><link rel="prefetch" href="/assets/js/336.c6384990.js"><link rel="prefetch" href="/assets/js/337.ab9d5e52.js"><link rel="prefetch" href="/assets/js/338.598ae59e.js"><link rel="prefetch" href="/assets/js/339.9b810ff8.js"><link rel="prefetch" href="/assets/js/34.3bd60f1f.js"><link rel="prefetch" href="/assets/js/340.5609a53c.js"><link rel="prefetch" href="/assets/js/341.969d335a.js"><link rel="prefetch" href="/assets/js/342.43f85dba.js"><link rel="prefetch" href="/assets/js/343.47280ef9.js"><link rel="prefetch" href="/assets/js/344.346ae5fc.js"><link rel="prefetch" href="/assets/js/345.32166361.js"><link rel="prefetch" href="/assets/js/346.86ff128b.js"><link rel="prefetch" href="/assets/js/347.3c5421fe.js"><link rel="prefetch" href="/assets/js/348.95910300.js"><link rel="prefetch" href="/assets/js/349.004158d8.js"><link rel="prefetch" href="/assets/js/35.18e8f66a.js"><link rel="prefetch" href="/assets/js/350.e10b195b.js"><link rel="prefetch" href="/assets/js/351.12f89875.js"><link rel="prefetch" href="/assets/js/352.83957394.js"><link rel="prefetch" href="/assets/js/353.475971b9.js"><link rel="prefetch" href="/assets/js/354.8af7b26b.js"><link rel="prefetch" href="/assets/js/355.85925e24.js"><link rel="prefetch" href="/assets/js/356.1d77cf9c.js"><link rel="prefetch" href="/assets/js/357.b467d481.js"><link rel="prefetch" href="/assets/js/358.ac96f32d.js"><link rel="prefetch" href="/assets/js/359.e048bd10.js"><link rel="prefetch" href="/assets/js/36.a7ae257c.js"><link rel="prefetch" href="/assets/js/360.e9b45545.js"><link rel="prefetch" href="/assets/js/361.ad1bb45b.js"><link rel="prefetch" href="/assets/js/362.4e24a30b.js"><link rel="prefetch" href="/assets/js/363.64f7dad5.js"><link rel="prefetch" href="/assets/js/364.d9e3ebc2.js"><link rel="prefetch" href="/assets/js/365.330e3086.js"><link rel="prefetch" href="/assets/js/366.a6c0afb3.js"><link rel="prefetch" href="/assets/js/367.ffcefa40.js"><link rel="prefetch" href="/assets/js/368.664c8e4d.js"><link rel="prefetch" href="/assets/js/369.67da2dd2.js"><link rel="prefetch" href="/assets/js/37.d709f9a9.js"><link rel="prefetch" href="/assets/js/370.b1791970.js"><link rel="prefetch" href="/assets/js/371.743a461a.js"><link rel="prefetch" href="/assets/js/372.7703ef1b.js"><link rel="prefetch" href="/assets/js/373.17de31f6.js"><link rel="prefetch" href="/assets/js/374.e508be9e.js"><link rel="prefetch" href="/assets/js/375.6e2e9fe3.js"><link rel="prefetch" href="/assets/js/376.8ca3511a.js"><link rel="prefetch" href="/assets/js/377.bfcee39c.js"><link rel="prefetch" href="/assets/js/378.21852b78.js"><link rel="prefetch" href="/assets/js/379.9649c307.js"><link rel="prefetch" href="/assets/js/38.68ca920e.js"><link rel="prefetch" href="/assets/js/380.dadb4418.js"><link rel="prefetch" href="/assets/js/381.a03c993d.js"><link rel="prefetch" href="/assets/js/382.bb7c22c1.js"><link rel="prefetch" href="/assets/js/383.bd68b2e5.js"><link rel="prefetch" href="/assets/js/384.f6dc7457.js"><link rel="prefetch" href="/assets/js/385.14287a91.js"><link rel="prefetch" href="/assets/js/386.a6284ac2.js"><link rel="prefetch" href="/assets/js/387.c51f147a.js"><link rel="prefetch" href="/assets/js/388.06d9651e.js"><link rel="prefetch" href="/assets/js/389.2d85d927.js"><link rel="prefetch" href="/assets/js/39.de850db9.js"><link rel="prefetch" href="/assets/js/390.2397b3e0.js"><link rel="prefetch" href="/assets/js/391.8413aaaf.js"><link rel="prefetch" href="/assets/js/392.00eecaa4.js"><link rel="prefetch" href="/assets/js/393.351dd3fd.js"><link rel="prefetch" href="/assets/js/394.d2cc4a70.js"><link rel="prefetch" href="/assets/js/395.abee64bf.js"><link rel="prefetch" href="/assets/js/396.bc265e6a.js"><link rel="prefetch" href="/assets/js/397.e4a96944.js"><link rel="prefetch" href="/assets/js/398.f0d26b29.js"><link rel="prefetch" href="/assets/js/399.e77c3ddc.js"><link rel="prefetch" href="/assets/js/4.2f502b73.js"><link rel="prefetch" href="/assets/js/40.91635261.js"><link rel="prefetch" href="/assets/js/400.c7eac401.js"><link rel="prefetch" href="/assets/js/401.06092d16.js"><link rel="prefetch" href="/assets/js/402.4e08e496.js"><link rel="prefetch" href="/assets/js/403.deba4c77.js"><link rel="prefetch" href="/assets/js/404.9100e4df.js"><link rel="prefetch" href="/assets/js/405.e9f451e2.js"><link rel="prefetch" href="/assets/js/406.a0da4aa4.js"><link rel="prefetch" href="/assets/js/407.58b2b123.js"><link rel="prefetch" href="/assets/js/408.eb2cde2d.js"><link rel="prefetch" href="/assets/js/409.9041f749.js"><link rel="prefetch" href="/assets/js/41.169a3cfc.js"><link rel="prefetch" href="/assets/js/410.c12f3710.js"><link rel="prefetch" href="/assets/js/411.452cda45.js"><link rel="prefetch" href="/assets/js/412.8691317a.js"><link rel="prefetch" href="/assets/js/413.25a68f21.js"><link rel="prefetch" href="/assets/js/414.7a9699d8.js"><link rel="prefetch" href="/assets/js/415.6e8cdaff.js"><link rel="prefetch" href="/assets/js/416.e8a86529.js"><link rel="prefetch" href="/assets/js/417.28330bde.js"><link rel="prefetch" href="/assets/js/418.2fed8e17.js"><link rel="prefetch" href="/assets/js/419.773032f4.js"><link rel="prefetch" href="/assets/js/42.e17cd061.js"><link rel="prefetch" href="/assets/js/420.56681228.js"><link rel="prefetch" href="/assets/js/421.0d170e13.js"><link rel="prefetch" href="/assets/js/422.fc8c2a11.js"><link rel="prefetch" href="/assets/js/423.e34ea6a6.js"><link rel="prefetch" href="/assets/js/424.20fe8748.js"><link rel="prefetch" href="/assets/js/425.f001b7cd.js"><link rel="prefetch" href="/assets/js/426.a91230ef.js"><link rel="prefetch" href="/assets/js/427.c6c9fe8d.js"><link rel="prefetch" href="/assets/js/428.9d4f5e41.js"><link rel="prefetch" href="/assets/js/429.c81cfd3f.js"><link rel="prefetch" href="/assets/js/43.f88aa667.js"><link rel="prefetch" href="/assets/js/430.eb61372f.js"><link rel="prefetch" href="/assets/js/431.4a25365a.js"><link rel="prefetch" href="/assets/js/432.42fc1bbe.js"><link rel="prefetch" href="/assets/js/433.8e9ac9f0.js"><link rel="prefetch" href="/assets/js/434.fdd0b160.js"><link rel="prefetch" href="/assets/js/435.d6cbac31.js"><link rel="prefetch" href="/assets/js/436.17750ba2.js"><link rel="prefetch" href="/assets/js/437.a1468099.js"><link rel="prefetch" href="/assets/js/438.ac6a45c2.js"><link rel="prefetch" href="/assets/js/439.242ae27a.js"><link rel="prefetch" href="/assets/js/44.3f36e228.js"><link rel="prefetch" href="/assets/js/440.b80b520c.js"><link rel="prefetch" href="/assets/js/441.e51a4bbb.js"><link rel="prefetch" href="/assets/js/442.ab0b3eda.js"><link rel="prefetch" href="/assets/js/443.47e44250.js"><link rel="prefetch" href="/assets/js/444.54c3b425.js"><link rel="prefetch" href="/assets/js/445.e006c12e.js"><link rel="prefetch" href="/assets/js/446.f2b79730.js"><link rel="prefetch" href="/assets/js/447.2509a397.js"><link rel="prefetch" href="/assets/js/448.b7d69dfc.js"><link rel="prefetch" href="/assets/js/449.8a575d9b.js"><link rel="prefetch" href="/assets/js/45.be1455b1.js"><link rel="prefetch" href="/assets/js/450.ad421aeb.js"><link rel="prefetch" href="/assets/js/451.f09912ae.js"><link rel="prefetch" href="/assets/js/452.f528c4ce.js"><link rel="prefetch" href="/assets/js/453.fc4dc0f9.js"><link rel="prefetch" href="/assets/js/454.733c315c.js"><link rel="prefetch" href="/assets/js/455.44fe35b1.js"><link rel="prefetch" href="/assets/js/456.7f650035.js"><link rel="prefetch" href="/assets/js/457.77ebc183.js"><link rel="prefetch" href="/assets/js/458.133078f4.js"><link rel="prefetch" href="/assets/js/459.33728b5e.js"><link rel="prefetch" href="/assets/js/46.5ad74cb5.js"><link rel="prefetch" href="/assets/js/460.6946f564.js"><link rel="prefetch" href="/assets/js/461.6f906c5c.js"><link rel="prefetch" href="/assets/js/462.26eca27e.js"><link rel="prefetch" href="/assets/js/463.e02e5f65.js"><link rel="prefetch" href="/assets/js/464.0df25c63.js"><link rel="prefetch" href="/assets/js/465.80cd249e.js"><link rel="prefetch" href="/assets/js/466.a0921e86.js"><link rel="prefetch" href="/assets/js/467.1bec2b47.js"><link rel="prefetch" href="/assets/js/468.b6fa37c1.js"><link rel="prefetch" href="/assets/js/469.a1d2487e.js"><link rel="prefetch" href="/assets/js/47.03df5d0a.js"><link rel="prefetch" href="/assets/js/470.36a4b9c8.js"><link rel="prefetch" href="/assets/js/471.28895273.js"><link rel="prefetch" href="/assets/js/472.b9fe392f.js"><link rel="prefetch" href="/assets/js/473.a2835d9c.js"><link rel="prefetch" href="/assets/js/474.cd57b37d.js"><link rel="prefetch" href="/assets/js/475.8d5a9792.js"><link rel="prefetch" href="/assets/js/476.f7f3781c.js"><link rel="prefetch" href="/assets/js/477.9b710b04.js"><link rel="prefetch" href="/assets/js/478.46d66687.js"><link rel="prefetch" href="/assets/js/479.e7444705.js"><link rel="prefetch" href="/assets/js/480.da2b750a.js"><link rel="prefetch" href="/assets/js/481.838a8e04.js"><link rel="prefetch" href="/assets/js/482.02bf8041.js"><link rel="prefetch" href="/assets/js/483.bb29ef2f.js"><link rel="prefetch" href="/assets/js/484.aebb68bd.js"><link rel="prefetch" href="/assets/js/485.a66ab41b.js"><link rel="prefetch" href="/assets/js/486.8df516fb.js"><link rel="prefetch" href="/assets/js/487.86d28130.js"><link rel="prefetch" href="/assets/js/488.0863da70.js"><link rel="prefetch" href="/assets/js/489.5d4d9e88.js"><link rel="prefetch" href="/assets/js/49.1ca52d11.js"><link rel="prefetch" href="/assets/js/490.3fd65d85.js"><link rel="prefetch" href="/assets/js/491.ae3a2f87.js"><link rel="prefetch" href="/assets/js/492.6f54679b.js"><link rel="prefetch" href="/assets/js/493.a594b1f4.js"><link rel="prefetch" href="/assets/js/494.4f03fae1.js"><link rel="prefetch" href="/assets/js/495.ae50b66b.js"><link rel="prefetch" href="/assets/js/496.bc92d835.js"><link rel="prefetch" href="/assets/js/497.eac65251.js"><link rel="prefetch" href="/assets/js/498.1d726726.js"><link rel="prefetch" href="/assets/js/499.01e4f7d1.js"><link rel="prefetch" href="/assets/js/5.7b1f056c.js"><link rel="prefetch" href="/assets/js/50.73732fe2.js"><link rel="prefetch" href="/assets/js/500.062ade4e.js"><link rel="prefetch" href="/assets/js/501.e6a711dc.js"><link rel="prefetch" href="/assets/js/502.c79e28e2.js"><link rel="prefetch" href="/assets/js/503.bf97bce9.js"><link rel="prefetch" href="/assets/js/504.b2784ef0.js"><link rel="prefetch" href="/assets/js/505.0767ba54.js"><link rel="prefetch" href="/assets/js/506.8ae14637.js"><link rel="prefetch" href="/assets/js/507.003e2349.js"><link rel="prefetch" href="/assets/js/508.ecb5e6bd.js"><link rel="prefetch" href="/assets/js/509.b644f44a.js"><link rel="prefetch" href="/assets/js/51.bed7cf61.js"><link rel="prefetch" href="/assets/js/510.b660946f.js"><link rel="prefetch" href="/assets/js/511.cb186c8f.js"><link rel="prefetch" href="/assets/js/512.d176170f.js"><link rel="prefetch" href="/assets/js/513.18a1afa5.js"><link rel="prefetch" href="/assets/js/514.4a33e931.js"><link rel="prefetch" href="/assets/js/515.8496bd4d.js"><link rel="prefetch" href="/assets/js/516.529b9476.js"><link rel="prefetch" href="/assets/js/517.289738f1.js"><link rel="prefetch" href="/assets/js/518.eda39556.js"><link rel="prefetch" href="/assets/js/519.85b42de5.js"><link rel="prefetch" href="/assets/js/52.22a7da62.js"><link rel="prefetch" href="/assets/js/520.8b531775.js"><link rel="prefetch" href="/assets/js/521.8e4b66fb.js"><link rel="prefetch" href="/assets/js/522.050bfc55.js"><link rel="prefetch" href="/assets/js/523.e973f1e0.js"><link rel="prefetch" href="/assets/js/524.fd160738.js"><link rel="prefetch" href="/assets/js/525.5a4bc307.js"><link rel="prefetch" href="/assets/js/526.361bc4fa.js"><link rel="prefetch" href="/assets/js/527.06ee9d73.js"><link rel="prefetch" href="/assets/js/528.22d9b118.js"><link rel="prefetch" href="/assets/js/529.b7592c16.js"><link rel="prefetch" href="/assets/js/53.767f3bbf.js"><link rel="prefetch" href="/assets/js/530.ad96d564.js"><link rel="prefetch" href="/assets/js/531.61905243.js"><link rel="prefetch" href="/assets/js/532.11aebf61.js"><link rel="prefetch" href="/assets/js/533.0cddb226.js"><link rel="prefetch" href="/assets/js/534.d1c9cc36.js"><link rel="prefetch" href="/assets/js/54.d1f9eec8.js"><link rel="prefetch" href="/assets/js/55.aa90e812.js"><link rel="prefetch" href="/assets/js/56.4d7f81f2.js"><link rel="prefetch" href="/assets/js/57.3c540e8d.js"><link rel="prefetch" href="/assets/js/58.fc1c23f2.js"><link rel="prefetch" href="/assets/js/59.c7a7a4c0.js"><link rel="prefetch" href="/assets/js/6.056886f1.js"><link rel="prefetch" href="/assets/js/60.40e590ae.js"><link rel="prefetch" href="/assets/js/61.e400b323.js"><link rel="prefetch" href="/assets/js/62.bbb30ef1.js"><link rel="prefetch" href="/assets/js/63.c31cd2bb.js"><link rel="prefetch" href="/assets/js/64.62321188.js"><link rel="prefetch" href="/assets/js/65.44adc2b3.js"><link rel="prefetch" href="/assets/js/66.b5740d4f.js"><link rel="prefetch" href="/assets/js/67.76c3624c.js"><link rel="prefetch" href="/assets/js/68.6dd982d4.js"><link rel="prefetch" href="/assets/js/69.eb1390ec.js"><link rel="prefetch" href="/assets/js/7.a96cecf9.js"><link rel="prefetch" href="/assets/js/70.8c882bdd.js"><link rel="prefetch" href="/assets/js/71.001c968a.js"><link rel="prefetch" href="/assets/js/72.ae4ca075.js"><link rel="prefetch" href="/assets/js/73.d6ae6a27.js"><link rel="prefetch" href="/assets/js/74.fba60e37.js"><link rel="prefetch" href="/assets/js/75.0b1bf7bf.js"><link rel="prefetch" href="/assets/js/76.ee19d90e.js"><link rel="prefetch" href="/assets/js/77.fe8ac5f0.js"><link rel="prefetch" href="/assets/js/78.ae09432e.js"><link rel="prefetch" href="/assets/js/79.c1b10e2d.js"><link rel="prefetch" href="/assets/js/80.ae809538.js"><link rel="prefetch" href="/assets/js/81.ca11b4c7.js"><link rel="prefetch" href="/assets/js/82.308b98e4.js"><link rel="prefetch" href="/assets/js/83.355d9dde.js"><link rel="prefetch" href="/assets/js/84.a2b61d48.js"><link rel="prefetch" href="/assets/js/85.cf992fda.js"><link rel="prefetch" href="/assets/js/86.f547c7ea.js"><link rel="prefetch" href="/assets/js/87.2b02746a.js"><link rel="prefetch" href="/assets/js/88.e939aad5.js"><link rel="prefetch" href="/assets/js/89.b000c42a.js"><link rel="prefetch" href="/assets/js/90.dadef001.js"><link rel="prefetch" href="/assets/js/91.c0fe2e32.js"><link rel="prefetch" href="/assets/js/92.e09650b9.js"><link rel="prefetch" href="/assets/js/93.427c7280.js"><link rel="prefetch" href="/assets/js/94.90fc5f58.js"><link rel="prefetch" href="/assets/js/95.13d42b3a.js"><link rel="prefetch" href="/assets/js/96.d0a2ea8c.js"><link rel="prefetch" href="/assets/js/97.cc9a0eda.js"><link rel="prefetch" href="/assets/js/98.3c9f1b7e.js"><link rel="prefetch" href="/assets/js/99.0420dcbc.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.33b2b47d.js">
    <link rel="stylesheet" href="/assets/css/0.styles.40c15e22.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="鱼皮的编程宝典" class="logo"> <span class="site-name can-hide">鱼皮的编程宝典</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/学习路线/" class="nav-link">
  学习路线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="自学之路" class="dropdown-title"><span class="title">自学之路</span> <span class="arrow down"></span></button> <button type="button" aria-label="自学之路" class="mobile-dropdown-title"><span class="title">自学之路</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/自学之路/#大学经历" class="nav-link">
  大学经历
</a></li><li class="dropdown-item"><!----> <a href="/自学之路/#求职经历" class="nav-link">
  求职经历
</a></li><li class="dropdown-item"><!----> <a href="/自学之路/#职场工作" class="nav-link">
  职场工作
</a></li><li class="dropdown-item"><!----> <a href="/自学之路/#创作经历" class="nav-link">
  创作经历
</a></li><li class="dropdown-item"><!----> <a href="/自学之路/#创业经历" class="nav-link">
  创业经历
</a></li><li class="dropdown-item"><!----> <a href="/自学之路/#生活日常" class="nav-link">
  生活日常
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="编程分享" class="dropdown-title"><span class="title">编程分享</span> <span class="arrow down"></span></button> <button type="button" aria-label="编程分享" class="mobile-dropdown-title"><span class="title">编程分享</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/编程分享/#入门必看-学习路线" class="nav-link">
  入门必看-学习路线
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#学习指南" class="nav-link">
  学习指南
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#开发经验" class="nav-link">
  开发经验
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#求职经验" class="nav-link">
  求职经验
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#职场经验" class="nav-link">
  职场经验
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#技术分享" class="nav-link">
  技术分享
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#实战教程" class="nav-link">
  实战教程
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#其他" class="nav-link">
  其他
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#📚-项目教程" class="nav-link">
  项目教程
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#个人作品" class="nav-link">
  个人作品
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#🎁-编程资源" class="nav-link">
  编程资源
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#🌐-科技科普" class="nav-link">
  科技科普
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="项目实战" class="dropdown-title"><span class="title">项目实战</span> <span class="arrow down"></span></button> <button type="button" aria-label="项目实战" class="mobile-dropdown-title"><span class="title">项目实战</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/项目实战/OJ 判题系统.html" class="nav-link">
  OJ 判题系统
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/智能 BI 平台.html" class="nav-link">
  智能 BI 平台
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/聚合搜索平台.html" class="nav-link">
  聚合搜索平台
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/API 开放平台.html" class="nav-link">
  API 开放平台
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/伙伴匹配系统.html" class="nav-link">
  伙伴匹配系统
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/用户中心项目.html" class="nav-link">
  用户中心项目
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/Java 后端万用项目模板.html" class="nav-link">
  Java 后端万用项目模板
</a></li></ul></div></div><div class="nav-item"><a href="/知识碎片/" class="nav-link">
  知识碎片
</a></div><div class="nav-item"><a href="/编程导航/" class="nav-link">
  🔥 编程导航
</a></div><div class="nav-item"><a href="/产品服务/" class="nav-link">
  产品服务
</a></div><div class="nav-item"><a href="/作者/" class="nav-link">
  作者
</a></div> <a href="https://github.com/liyupi/codefather" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/学习路线/" class="nav-link">
  学习路线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="自学之路" class="dropdown-title"><span class="title">自学之路</span> <span class="arrow down"></span></button> <button type="button" aria-label="自学之路" class="mobile-dropdown-title"><span class="title">自学之路</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/自学之路/#大学经历" class="nav-link">
  大学经历
</a></li><li class="dropdown-item"><!----> <a href="/自学之路/#求职经历" class="nav-link">
  求职经历
</a></li><li class="dropdown-item"><!----> <a href="/自学之路/#职场工作" class="nav-link">
  职场工作
</a></li><li class="dropdown-item"><!----> <a href="/自学之路/#创作经历" class="nav-link">
  创作经历
</a></li><li class="dropdown-item"><!----> <a href="/自学之路/#创业经历" class="nav-link">
  创业经历
</a></li><li class="dropdown-item"><!----> <a href="/自学之路/#生活日常" class="nav-link">
  生活日常
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="编程分享" class="dropdown-title"><span class="title">编程分享</span> <span class="arrow down"></span></button> <button type="button" aria-label="编程分享" class="mobile-dropdown-title"><span class="title">编程分享</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/编程分享/#入门必看-学习路线" class="nav-link">
  入门必看-学习路线
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#学习指南" class="nav-link">
  学习指南
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#开发经验" class="nav-link">
  开发经验
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#求职经验" class="nav-link">
  求职经验
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#职场经验" class="nav-link">
  职场经验
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#技术分享" class="nav-link">
  技术分享
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#实战教程" class="nav-link">
  实战教程
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#其他" class="nav-link">
  其他
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#📚-项目教程" class="nav-link">
  项目教程
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#个人作品" class="nav-link">
  个人作品
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#🎁-编程资源" class="nav-link">
  编程资源
</a></li><li class="dropdown-item"><!----> <a href="/编程分享/#🌐-科技科普" class="nav-link">
  科技科普
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="项目实战" class="dropdown-title"><span class="title">项目实战</span> <span class="arrow down"></span></button> <button type="button" aria-label="项目实战" class="mobile-dropdown-title"><span class="title">项目实战</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/项目实战/OJ 判题系统.html" class="nav-link">
  OJ 判题系统
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/智能 BI 平台.html" class="nav-link">
  智能 BI 平台
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/聚合搜索平台.html" class="nav-link">
  聚合搜索平台
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/API 开放平台.html" class="nav-link">
  API 开放平台
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/伙伴匹配系统.html" class="nav-link">
  伙伴匹配系统
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/用户中心项目.html" class="nav-link">
  用户中心项目
</a></li><li class="dropdown-item"><!----> <a href="/项目实战/Java 后端万用项目模板.html" class="nav-link">
  Java 后端万用项目模板
</a></li></ul></div></div><div class="nav-item"><a href="/知识碎片/" class="nav-link">
  知识碎片
</a></div><div class="nav-item"><a href="/编程导航/" class="nav-link">
  🔥 编程导航
</a></div><div class="nav-item"><a href="/产品服务/" class="nav-link">
  产品服务
</a></div><div class="nav-item"><a href="/作者/" class="nav-link">
  作者
</a></div> <a href="https://github.com/liyupi/codefather" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/%E7%9F%A5%E8%AF%86%E7%A2%8E%E7%89%87/" class="sidebar-link">编程导航知识碎片</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>知识碎片</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/_4-%E7%A7%8D%E6%96%B9%E6%B3%95-%E5%BF%AB%E9%80%9F%E5%88%9D%E5%A7%8B%E5%8C%96-java-%E9%A1%B9%E7%9B%AE/" class="sidebar-link">4 种方法，快速初始化 Java 项目</a></li><li><a href="/%E4%BD%BF%E7%94%A8%E5%90%8E%E7%AB%AF%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90%E5%99%A8-%E6%8F%90%E9%AB%98%E5%BC%80%E5%8F%91%E6%95%88%E7%8E%87/" class="sidebar-link">使用后端代码生成器，提高开发效率</a></li><li><a href="/%E5%89%8D%E7%AB%AF%E5%BF%85%E5%AD%A6%E7%9A%84%E5%BC%80%E5%8F%91%E6%A1%86%E6%9E%B6-ant-design-pro/" class="sidebar-link">前端必学的开发框架，Ant Design Pro</a></li><li><a href="/%E5%90%8E%E7%AB%AF%E5%A6%82%E4%BD%95%E7%BC%96%E5%86%99%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/" class="sidebar-link">后端如何编写单元测试？</a></li><li><a href="/%E5%90%8E%E7%AB%AF%E9%A1%B9%E7%9B%AE%E7%BB%8F%E5%85%B8%E5%88%86%E5%B1%82%E6%9E%B6%E6%9E%84%E4%BB%8B%E7%BB%8D/" class="sidebar-link">后端项目经典分层架构介绍</a></li><li><a href="/%E5%A6%82%E4%BD%95%E9%AB%98%E6%95%88%E6%B5%8B%E8%AF%95%E6%8E%A5%E5%8F%A3-%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3/" class="sidebar-link">如何高效测试接口？自动生成接口文档</a></li><li><a href="/%E5%BC%80%E5%8F%91%E4%BC%81%E4%B8%9A%E5%BE%AE%E4%BF%A1%E7%BE%A4%E6%9C%BA%E5%99%A8%E4%BA%BA-%E5%AE%9E%E7%8E%B0%E5%AE%9A%E6%97%B6%E6%8F%90%E9%86%92/" class="sidebar-link">开发企业微信群机器人，实现定时提醒</a></li><li><a href="/%E5%A6%82%E4%BD%95%E8%A7%A3%E5%86%B3%E6%95%B0%E6%8D%AE%E6%9F%A5%E8%AF%A2%E4%B8%BA%E7%A9%BA%E7%9A%84-bug/" class="sidebar-link">如何解决数据查询为空的 Bug？</a></li><li><a href="/git-%E6%8F%90%E4%BA%A4%E7%AB%9F%E7%84%B6%E8%BF%98%E8%83%BD%E8%BF%99%E4%B9%88%E7%94%A8/" class="sidebar-link">Git 提交竟然还能这么用？</a></li><li><a href="/%E3%80%90%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E3%80%91%E8%A3%85%E9%A5%B0%E8%80%85%E6%A8%A1%E5%BC%8F-%E5%8F%8A%E5%85%B6%E5%9C%A8jdk%E6%BA%90%E7%A0%81%E4%B8%AD%E7%9A%84%E5%BA%94%E7%94%A8/" class="sidebar-link">【设计模式】装饰者模式，及其在JDK源码中的应用</a></li><li><a href="/%E7%94%A8%E5%9B%9B%E7%A7%8D%E9%94%81%E5%AE%9E%E7%8E%B0%E5%8A%A0%E5%85%A5%E9%98%9F%E4%BC%8D%E5%8A%9F%E8%83%BD-%E4%BC%99%E4%BC%B4%E5%8C%B9%E9%85%8D%E7%B3%BB%E7%BB%9F/" class="sidebar-link">用四种锁实现加入队伍功能(伙伴匹配系统)</a></li><li><a href="/%E7%94%A8-validated%E6%B3%A8%E8%A7%A3%E5%AE%9E%E7%8E%B0%E9%9D%9E%E7%A9%BA%E6%A0%A1%E9%AA%8C/" class="sidebar-link">用@Validated注解实现非空校验</a></li><li><a href="/%E4%BD%BF%E7%94%A8%E7%AC%AC%E4%B8%89%E6%96%B9%E6%9C%8D%E5%8A%A1-%E5%AE%9D%E5%A1%94-%E5%BF%AB%E9%80%9F%E9%83%A8%E7%BD%B2%E9%A1%B9%E7%9B%AE/" class="sidebar-link">使用第三方服务（宝塔）快速部署项目</a></li><li><a href="/restful-%E6%8E%A5%E5%8F%A3%E5%AE%9E%E7%8E%B0%E4%B8%8E%E6%B5%8B%E8%AF%95-spring-boot/" class="sidebar-link">RESTful 接口实现与测试( Spring Boot )</a></li><li><a href="/%E5%BF%AB%E9%80%9F%E5%AD%A6%E4%BC%9A%E4%B8%BA%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE%E5%81%9A%E8%B4%A1%E7%8C%AE/" class="sidebar-link">快速学会为开源项目做贡献</a></li><li><a href="/%E6%95%B4%E5%90%88spring-jdbc%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E5%BA%93/" class="sidebar-link">整合Spring JDBC操作数据库</a></li><li><a href="/maven-%E4%B8%AD%E5%A4%AE%E4%BB%93%E5%BA%93%E5%8F%91%E5%8C%85%E6%B5%81%E7%A8%8B/" class="sidebar-link">Maven 中央仓库发包流程</a></li><li><a href="/nacos-%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%E6%90%AD%E5%BB%BA/" class="sidebar-link">Nacos 配置中心搭建</a></li><li><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/" aria-current="page" class="active sidebar-link">Bean 拷贝之 MapStruct</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#传统写法带来的困扰" class="sidebar-link">传统写法带来的困扰</a></li><li class="sidebar-sub-header"><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#mapstruct-的用法" class="sidebar-link">MapStruct 的用法</a></li><li class="sidebar-sub-header"><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#mapstruct-插件" class="sidebar-link">MapStruct 插件</a></li><li class="sidebar-sub-header"><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#mapstruct-的背后原理" class="sidebar-link">MapStruct 的背后原理</a></li></ul></li><li><a href="/%E4%BD%BF%E7%94%A8-freemarker-%E6%A8%A1%E6%9D%BF%E5%BC%95%E6%93%8E%E7%94%9F%E6%88%90%E4%BB%A3%E7%A0%81/" class="sidebar-link">使用 FreeMarker 模板引擎生成代码</a></li><li><a href="/%E5%A6%82%E4%BD%95%E5%BF%AB%E9%80%9F%E5%BF%BD%E7%95%A5-git-%E6%96%87%E4%BB%B6%E6%8F%90%E4%BA%A4/" class="sidebar-link">如何快速忽略 Git 文件提交？</a></li><li><a href="/easyexcel-%E5%AF%BC%E5%87%BA%E6%96%87%E4%BB%B6/" class="sidebar-link">EasyExcel 导出文件</a></li><li><a href="/%E5%A6%82%E4%BD%95%E5%A4%84%E7%90%86-completablefuture-%E4%B8%AD%E7%9A%84%E4%B8%A4%E7%A7%8D%E5%BC%82%E5%B8%B8-%E6%99%BA%E8%83%BDbi%E9%A1%B9%E7%9B%AE/" class="sidebar-link">如何处理 CompletableFuture 中的两种异常(智能BI项目)</a></li><li><a href="/%E5%9F%BA%E4%BA%8E-session-%E5%AE%9E%E7%8E%B0%E7%9F%AD%E4%BF%A1%E7%99%BB%E5%BD%95/" class="sidebar-link">基于 Session 实现短信登录</a></li><li><a href="/synchronized-%E5%85%B3%E9%94%AE%E5%AD%97%E8%AF%A6%E8%A7%A3/" class="sidebar-link">Synchronized 关键字详解</a></li><li><a href="/redission-%E8%A7%A3%E9%94%81%E5%BC%82%E5%B8%B8%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/" class="sidebar-link">Redission 解锁异常解决方案</a></li><li><a href="/%E5%AE%9E%E7%8E%B0%E4%B8%BB%E9%A2%98%E6%8D%A2%E8%82%A4-css-%E5%8F%98%E9%87%8F-%E7%B1%BB%E5%90%8D%E5%88%87%E6%8D%A2/" class="sidebar-link">实现主题换肤( CSS 变量 + 类名切换)</a></li><li><a href="/springboot-%E6%95%B4%E5%90%88-minio%E5%85%A8%E6%B5%81%E7%A8%8B-%E4%BB%8E%E5%AE%89%E8%A3%85-minio-%E5%88%B0%E5%BA%94%E7%94%A8/" class="sidebar-link">SpringBoot 整合 Minio全流程（从安装 Minio 到应用）</a></li><li><a href="/%E3%80%90redis%E5%BA%94%E7%94%A8%E3%80%91uv%E7%BB%9F%E8%AE%A1/" class="sidebar-link">【Redis应用】UV统计</a></li><li><a href="/%E3%80%8A%E8%B4%AF%E7%A9%BF%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E3%80%8B%E7%AC%AC%E4%B8%89%E6%96%B9%E7%99%BB%E5%BD%95%E5%AE%9E%E8%B7%B5-%E9%99%84%E5%89%8D%E5%90%8E%E7%AB%AF%E5%AE%8C%E6%95%B4%E8%BF%87%E7%A8%8B%E5%8F%8A%E6%B5%8B%E8%AF%95/" class="sidebar-link">《贯穿设计模式》第三方登录实践[附前后端完整过程及测试]</a></li><li><a href="/websocket-spring-sseemitter3-%E5%AE%9E%E7%8E%B0%E8%AE%AF%E9%A3%9E%E6%98%9F%E7%81%ABjava%E5%AE%A2%E6%88%B7%E7%AB%AF/" class="sidebar-link">Websocket.+ Spring-SseEmitter3 实现讯飞星火Java客户端</a></li><li><a href="/%E7%94%A8%E5%B8%83%E9%9A%86%E8%BF%87%E6%BB%A4%E5%99%A8%E5%AE%9E%E7%8E%B0%E6%A3%80%E6%9F%A5%E7%94%A8%E6%88%B7%E5%90%8D%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8-%E7%94%A8%E6%88%B7%E6%B3%A8%E5%86%8C/" class="sidebar-link">用布隆过滤器实现检查用户名是否存在(用户注册)</a></li><li><a href="/springsecurity-%E6%95%B4%E5%90%88-oauth2/" class="sidebar-link">SpringSecurity 整合 Oauth2</a></li><li><a href="/%E5%89%8D%E7%AB%AF%E5%88%9D%E5%A7%8B%E5%8C%96-ant-design-pro-%E7%AC%94%E8%AE%B0/" class="sidebar-link">前端初始化 Ant Design Pro 笔记</a></li><li><a href="/liteflow-%E7%BC%96%E6%8E%92%E5%BC%8F%E7%BC%96%E7%A8%8B%E7%AE%80%E5%8D%95%E4%BB%8B%E7%BB%8D/" class="sidebar-link">LiteFlow 编排式编程简单介绍</a></li><li><a href="/%E5%9F%BA%E4%BA%8E-redis-%E5%AE%9E%E7%8E%B0%E7%9F%AD%E4%BF%A1%E7%99%BB%E5%BD%95/" class="sidebar-link">基于 Redis 实现短信登录</a></li><li><a href="/mysql-%E4%B8%80%E6%A3%B5-b-%E6%A0%91%E5%8F%AF%E4%BB%A5%E5%AD%98%E5%A4%9A%E5%B0%91%E6%9D%A1%E6%95%B0%E6%8D%AE/" class="sidebar-link">MySQL 一棵 B + 树可以存多少条数据？</a></li><li><a href="/springboot-%E6%95%B4%E5%90%88-elk-%E5%AE%9E%E7%8E%B0%E6%97%A5%E5%BF%97%E9%87%87%E9%9B%86%E4%B8%8E%E7%9B%91%E6%8E%A7/" class="sidebar-link">SpringBoot 整合 ELK 实现日志采集与监控</a></li><li><a href="/%E5%9F%BA%E4%BA%8E%E8%87%AA%E5%AE%9A%E4%B9%89%E6%B3%A8%E8%A7%A3%E7%9A%84-redisson-%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81%E5%AE%9E%E7%8E%B0/" class="sidebar-link">基于自定义注解的 Redisson 分布式锁实现</a></li><li><a href="/%E7%8E%A9%E8%BD%AC%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B%E5%88%A9%E5%99%A8-completablefuture/" class="sidebar-link">玩转异步编程利器 CompletableFuture</a></li><li><a href="/%E9%80%9A%E8%BF%87-windows-%E8%84%9A%E6%9C%AC%E6%89%A7%E8%A1%8C%E6%89%93%E5%8C%85%E6%93%8D%E4%BD%9C/" class="sidebar-link">通过 Windows 脚本执行打包操作</a></li><li><a href="/%E9%98%BF%E9%87%8C%E4%BA%91%E5%AF%B9%E8%B1%A1%E5%AD%98%E5%82%A8-oss/" class="sidebar-link">阿里云对象存储 OSS</a></li><li><a href="/%E5%90%8E%E7%AB%AF-spring-boot-%E4%B8%87%E7%94%A8%E6%A8%A1%E6%9D%BF%E4%BD%BF%E7%94%A8/" class="sidebar-link">后端 Spring Boot 万用模板使用</a></li><li><a href="/%E4%BD%BF%E7%94%A8-canal-%E5%AE%9E%E7%8E%B0%E5%A2%9E%E9%87%8F%E8%AE%A2%E9%98%85%E5%92%8C%E6%B6%88%E8%B4%B9/" class="sidebar-link">使用 canal 实现增量订阅和消费</a></li><li><a href="/springboot-quartz-%E7%AE%80%E6%98%93%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/" class="sidebar-link">SpringBoot + Quartz 简易定时任务</a></li><li><a href="/string-%E7%9A%84-intern-%E6%96%B9%E6%B3%95/" class="sidebar-link">String 的 intern() 方法</a></li><li><a href="/mysql-%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%97%A5%E6%9C%9F%E6%A0%BC%E5%BC%8F%E8%BD%AC%E6%8D%A2/" class="sidebar-link">MySQL 字符串日期格式转换</a></li><li><a href="/fail-fast-%E6%9C%BA%E5%88%B6%E6%98%AF%E4%BB%80%E4%B9%88/" class="sidebar-link">fail-fast 机制是什么？</a></li><li><a href="/%E5%8D%81%E7%A7%8D-sql-%E7%9A%84%E5%86%99%E6%B3%95/" class="sidebar-link">十种 SQL 的写法</a></li><li><a href="/mybatis-%E6%95%B4%E5%90%88%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90/" class="sidebar-link">MyBatis 整合多数据源</a></li><li><a href="/_1%E7%A7%92%E5%B0%86%E6%9C%AC%E5%9C%B0springboot%E9%A1%B9%E7%9B%AEjar%E5%8C%85%E9%83%A8%E7%BD%B2%E5%88%B0linux%E7%8E%AF%E5%A2%83/" class="sidebar-link">1秒将本地SpringBoot项目jar包部署到Linux环境</a></li><li><a href="/ddd-%E6%98%AF%E4%BB%80%E4%B9%88/" class="sidebar-link">DDD 是什么？</a></li><li><a href="/oracle%E5%88%B0mysql%E5%87%BD%E6%95%B0%E6%9B%BF%E6%8D%A2%E6%96%B9%E6%A1%88%E6%B1%87%E6%80%BB/" class="sidebar-link">Oracle 到 MySQL 函数替换方案汇总</a></li><li><a href="/springboot-%E5%BC%95%E5%85%A5-nacos-windows-%E7%89%88/" class="sidebar-link">Springboot 引入 Nacos ( Windows 版)</a></li><li><a href="/%E5%88%A9%E7%94%A8%E5%B1%80%E5%9F%9F%E7%BD%91%E6%90%AD%E5%BB%BA%E8%99%9A%E6%8B%9F%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%AE%BF%E9%97%AE%E9%A1%B9%E7%9B%AE/" class="sidebar-link">利用局域网搭建虚拟机实现访问项目</a></li><li><a href="/%E6%B5%85%E8%B0%88-cookie-%E5%92%8C-session/" class="sidebar-link">浅谈 cookie 和 session</a></li><li><a href="/%E7%94%A8-qq-%E9%82%AE%E7%AE%B1%E5%AE%9E%E7%8E%B0%E9%AA%8C%E8%AF%81%E7%A0%81%E5%8A%9F%E8%83%BD/" class="sidebar-link">用 QQ 邮箱实现验证码功能</a></li><li><a href="/redis-%E5%AE%9E%E7%8E%B0%E6%96%87%E7%AB%A0%E7%82%B9%E8%B5%9E%E5%8A%9F%E8%83%BD-%E9%99%84%E5%B8%A6%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BB%A3%E7%A0%81%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93/" class="sidebar-link">Redis 实现文章点赞功能(附带前后端代码、数据库)</a></li><li><a href="/%E9%98%BF%E9%87%8C%E4%BA%91%E7%9F%AD%E4%BF%A1%E6%9C%8D%E5%8A%A1%E5%AE%9E%E7%8E%B0%E6%89%8B%E6%9C%BA%E9%AA%8C%E8%AF%81%E7%A0%81/" class="sidebar-link">阿里云短信服务实现手机验证码</a></li><li><a href="/%E3%80%90ajax%E3%80%91%E5%BC%82%E6%AD%A5%E9%80%9A%E4%BF%A1/" class="sidebar-link">【Ajax】异步通信</a></li><li><a href="/%E3%80%90%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E3%80%91%E4%B8%83%E5%A4%A7%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99/" class="sidebar-link">【设计模式】七大设计原则</a></li><li><a href="/%E6%96%87%E7%AB%A0%E8%AF%84%E8%AE%BA%E5%8A%9F%E8%83%BD%E5%89%8D%E5%90%8E%E7%AB%AF%E5%AE%9E%E7%8E%B0%E6%96%B9%E6%A1%88%E6%80%BB%E7%BB%93/" class="sidebar-link">文章评论功能前后端实现方案总结</a></li><li><a href="/%E3%80%90%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E3%80%91git%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/" class="sidebar-link">【版本控制】Git快速上手</a></li><li><a href="/%E3%80%90java%E5%9F%BA%E7%A1%80%E3%80%91%E6%B6%88%E7%81%AD%E9%AD%94%E6%B3%95%E5%80%BC-%E5%B8%B8%E9%87%8F-%E6%9E%9A%E4%B8%BE%E8%AF%A6%E8%BF%B0/" class="sidebar-link">【Java基础】消灭魔法值-常量&amp;枚举详述</a></li><li><a href="/mysql-%E7%9A%84-char-%E5%B9%B6%E4%B8%8D%E4%B8%80%E5%AE%9A%E6%98%AF%E5%AE%9A%E9%95%BF/" class="sidebar-link">MySQL 的 Char 并不一定是定长</a></li><li><a href="/textcnn-%E6%96%87%E6%9C%AC%E5%88%86%E7%B1%BB%E6%A8%A1%E5%9E%8B%E5%9C%A8%E6%96%87%E7%AB%A0%E8%AF%84%E8%AE%BA%E5%AE%A1%E6%A0%B8%E4%B8%AD%E7%9A%84%E5%AE%9E%E7%8E%B0%E4%B8%8E%E9%83%A8%E7%BD%B2/" class="sidebar-link">TextCNN 文本分类模型在文章评论审核中的实现与部署</a></li><li><a href="/%E7%90%86%E8%A7%A3-io-%E5%A4%9A%E8%B7%AF%E5%A4%8D%E7%94%A8/" class="sidebar-link">理解 IO 多路复用</a></li><li><a href="/elasticsearch-%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%85%A5%E9%97%A8%E4%BD%BF%E7%94%A8/" class="sidebar-link">ElasticSearch 基础概念与入门使用</a></li><li><a href="/%E7%90%86%E8%A7%A3%E8%BF%9B%E7%A8%8B-%E7%BA%BF%E7%A8%8B-%E5%8D%8F%E7%A8%8B/" class="sidebar-link">理解进程，线程，协程</a></li><li><a href="/%E7%90%86%E8%A7%A3%E5%8E%9F%E7%A0%81%E3%80%81%E5%8F%8D%E7%A0%81%E3%80%81%E8%A1%A5%E7%A0%81/" class="sidebar-link">理解原码、反码、补码</a></li><li><a href="/%E5%AE%9A%E4%B9%89api%E5%B9%B6%E7%94%9F%E6%88%90%E4%BB%A3%E7%A0%81-go-%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%A1%86%E6%9E%B6-kratos/" class="sidebar-link">定义API并生成代码（ Go 微服务框架 Kratos ）</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="content"><div style="width:100%"><div class="theme-default-content custom-content content__default"><h1 id="bean-拷贝之-mapstruct"><a href="#bean-拷贝之-mapstruct" class="header-anchor">#</a> Bean 拷贝之 MapStruct</h1> <blockquote><p>作者：<a href="https://juejin.cn/user/128017175944557" target="_blank" rel="noopener noreferrer">顾恙ツ<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，<a href="https://wx.zsxq.com/dweb2/index/group/51122858222824" target="_blank" rel="noopener noreferrer">编程导航星球<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>  编号 2784</p></blockquote> <blockquote><p>大家好，今天我给大家带来一个 Bean 拷贝的高效工具，MapSrtuct 。有人会问 MapSturct 是什么？简单来说，MapStruct 就是一个 Java Bean 的映射器，我们只需要在一个 XXXStructMapper 的接口中定义好方法，MapStruct 就会在编译的时候生成相应的实现类，这个实现类中包含了具体的映射代码，极大地提高了我们编码的效率，省去了大量的模板代码。</p></blockquote> <h2 id="传统写法带来的困扰"><a href="#传统写法带来的困扰" class="header-anchor">#</a> 传统写法带来的困扰</h2> <p>假如没有使用 MapStruct 的话，当我们需要把 DO 对象转成一个 DTO 对象时，我们需要这样做。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">ArticleDTO</span> <span class="token function">toDto</span><span class="token punctuation">(</span><span class="token class-name">ArticleDO</span> articleDO<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>articleDO <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">ArticleDTO</span> articleDTO <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArticleDTO</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setAuthor</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getUserId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setArticleId</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setArticleType</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getArticleType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setTitle</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getTitle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setShortTitle</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getShortTitle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setSummary</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getSummary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setCover</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getPicture</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setSourceType</span><span class="token punctuation">(</span><span class="token class-name">SourceTypeEnum</span><span class="token punctuation">.</span><span class="token function">formCode</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getDesc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setSourceUrl</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getSourceUrl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setStatus</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getStatus</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setCreateTime</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getCreateTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setLastUpdateTime</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getUpdateTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setOfficalStat</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getOfficalStat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setToppingStat</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getToppingStat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setCreamStat</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getCreamStat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 设置类目id</span>
    articleDTO<span class="token punctuation">.</span><span class="token function">setCategory</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">CategoryDTO</span><span class="token punctuation">(</span>articleDO<span class="token punctuation">.</span><span class="token function">getCategoryId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> articleDTO<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><p>如果是需要批量进行转化的话，这个时候还需要遍历进行批量转化。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ArticleDTO</span><span class="token punctuation">&gt;</span></span> <span class="token function">toArticleDtoList</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ArticleDO</span><span class="token punctuation">&gt;</span></span> articleDOS<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> articleDOS<span class="token punctuation">.</span><span class="token function">stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token class-name">ArticleConverter</span><span class="token operator">::</span><span class="token function">toDto</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token class-name">Collectors</span><span class="token punctuation">.</span><span class="token function">toList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>这样的代码如果只是一个方法可能还好，但是如果写多了，你就会发现代码不是很优雅，而且每次写代码量还巨大，虽然有插件可以帮你写，但是感觉还是不好，这个时候，肯定有人会说用 BeanUtils，这个时候我会给你大大的一巴掌，主要是 BeeanUtils 其可能会踩一些坑，你可能把握不是很好，这里我找了一些关于 BeanUtils 的一些文章，感兴趣的同学可以一起看看：<br>Spring的BeanUtils.copyProperties()避坑指南：<a href="https://juejin.cn/post/7012279747526787080" target="_blank" rel="noopener noreferrer">https://juejin.cn/post/7012279747526787080<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a><br>几个 BeanUtils 中的坑，千万别踩！<a href="https://cloud.tencent.com/developer/article/1522266" target="_blank" rel="noopener noreferrer">https://cloud.tencent.com/developer/article/1522266<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a><br>SpringBoot 整合mapstruct|赶紧丢掉BeanUtils吧：<a href="https://juejin.cn/post/7035161765948162078" target="_blank" rel="noopener noreferrer">https://juejin.cn/post/7035161765948162078<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a><br><img alt="" data-src="https://pic.yupi.icu/5563/202311081045996.jpeg" loading="lazy" class="lazy"></p> <p>那有什么简单的解决方法，那就是我们今天的主角， MapStruct 了，接下来我们简单使用一下 MapStruct演示一下。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapper</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ArticleStructMapper</span> <span class="token punctuation">{</span>
    <span class="token class-name">ArticleStructMapper</span> <span class="token constant">INSTANCE</span> <span class="token operator">=</span> <span class="token class-name">Mappers</span><span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span><span class="token class-name">ArticleStructMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">ArticleDTO</span> <span class="token function">toDTO</span><span class="token punctuation">(</span><span class="token class-name">ArticleDO</span> <span class="token keyword">do</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>定义了一个接口 ArticleStructMapper，该接口的主要作用是将 ArticleDO 对象转换为 ArticleDTO 对象。</p> <p>让我们一步步地分析代码：</p> <blockquote><ol><li><a href="https://www.yuque.com/Mapper" target="_blank" rel="noopener noreferrer"><strong>@Mapper</strong><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a><strong>：</strong></li></ol></blockquote> <p>这是 MapStruct 的核心注解之一。它标记了这个接口为一个映射器，并告诉 MapStruct 的注解处理器在编译时为此接口生成实现。</p> <blockquote><ol start="2"><li><strong>INSTANCE 常量</strong></li></ol></blockquote> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">ArticleStructMapper</span> <span class="token constant">INSTANCE</span> <span class="token operator">=</span> <span class="token class-name">Mappers</span><span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span> <span class="token class-name">ArticleStructMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>Mappers.getMapper 是 MapStruct 提供的一个工具方法，用于在不使用 Spring 或其他依赖注入框架的情况下获取映射器的实例。</p> <blockquote><ol start="3"><li><strong>toDTO 方法</strong>：</li></ol></blockquote> <p>这个方法定义了一个转换。这个大家都懂，我就不做过多的赘述了。</p> <h2 id="mapstruct-的用法"><a href="#mapstruct-的用法" class="header-anchor">#</a> MapStruct 的用法</h2> <h3 id="引入依赖"><a href="#引入依赖" class="header-anchor">#</a> 引入依赖</h3> <p>在 pom.xml 文件中引入 MapStruct 的 Maven 坐标,这里需要注意，引入 guava 的时候需要注意版本冲突问题，尽量引入大于 19.0 以上的版本，不然可能会报错。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span> 引入 mapstruct <span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token generics"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
    <span class="token generics"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>mapstruct<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
    <span class="token generics"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>mapstruct<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
 	<span class="token generics"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span><span class="token number">1.5</span><span class="token number">.5</span><span class="token punctuation">.</span>Final<span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>
<span class="token generics"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
    <span class="token generics"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>mapstruct<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
    <span class="token generics"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>mapstruct<span class="token operator">-</span>processor<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
	 <span class="token generics"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span><span class="token number">1.5</span><span class="token number">.5</span><span class="token punctuation">.</span>Final<span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>它们的作用如下：</p> <ol><li><strong>org.mapstruct:mapstruct</strong>：</li></ol> <ul><li>MapStruct 的核心库。它提供了 MapStruct 所需的主要注解和工具方法，例如 @Mapper, @Mapping 等注解以及 Mappers.getMapper() 方法。</li> <li>在运行时，这个库是必需的，生成的映射代码会依赖它。</li></ul> <ol start="2"><li><strong>org.mapstruct:mapstruct-processor</strong>：</li></ol> <ul><li>MapStruct 的注解处理器。它在编译时生成具体的映射实现代码。</li> <li>compile 作用域，意味着它只在编译时被使用。</li> <li>当你编译一个使用了 MapStruct 注解的项目时，注解处理器会检测你的代码，然后为你的 @Mapper 注解的接口或抽象类生成实现。</li></ul> <h3 id="定义映射器接口"><a href="#定义映射器接口" class="header-anchor">#</a> 定义映射器接口</h3> <p>像前面大家看到的 ArticleStructMapper 接口，我们稍微来一个复杂的。这里需要注意导入的 Mapper，是 MapStruct 的 Mapper ，而不是 MyBatis 下的 Mapper。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapper</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ColumnStructMapper</span> <span class="token punctuation">{</span>
    <span class="token class-name">ColumnStructMapper</span> <span class="token constant">INSTANCE</span> <span class="token operator">=</span> <span class="token class-name">Mappers</span><span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span> <span class="token class-name">ColumnStructMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">/**
     * ColumnInfoDO to ColumnDTO
     * @param columnInfoDO
     * @return
     */</span>
    <span class="token comment">// sources 是参数，target 是目标</span>
    <span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>source <span class="token operator">=</span> <span class="token string">&quot;id&quot;</span><span class="token punctuation">,</span> target <span class="token operator">=</span> <span class="token string">&quot;columnId&quot;</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>source <span class="token operator">=</span> <span class="token string">&quot;columnName&quot;</span><span class="token punctuation">,</span> target <span class="token operator">=</span> <span class="token string">&quot;column&quot;</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>source <span class="token operator">=</span> <span class="token string">&quot;userId&quot;</span><span class="token punctuation">,</span> target <span class="token operator">=</span> <span class="token string">&quot;author&quot;</span><span class="token punctuation">)</span>
    <span class="token comment">// Date 转 Long</span>
    <span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>target <span class="token operator">=</span> <span class="token string">&quot;publishTime&quot;</span><span class="token punctuation">,</span> expression <span class="token operator">=</span> <span class="token string">&quot;java(columnInfoDO.getPublishTime().getTime())&quot;</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>target <span class="token operator">=</span> <span class="token string">&quot;freeStartTime&quot;</span><span class="token punctuation">,</span> expression <span class="token operator">=</span> <span class="token string">&quot;java(columnInfoDO.getFreeStartTime().getTime())&quot;</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>target <span class="token operator">=</span> <span class="token string">&quot;freeEndTime&quot;</span><span class="token punctuation">,</span> expression <span class="token operator">=</span> <span class="token string">&quot;java(columnInfoDO.getFreeEndTime().getTime())&quot;</span><span class="token punctuation">)</span>
    <span class="token class-name">ColumnDTO</span> <span class="token function">infotoDto</span><span class="token punctuation">(</span><span class="token class-name">ColumnInfoDO</span> columnInfoDO<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ColumnDTO</span><span class="token punctuation">&gt;</span></span> <span class="token function">infoToDtos</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ColumnInfoDO</span><span class="token punctuation">&gt;</span></span> columnInfoDOs<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>source <span class="token operator">=</span> <span class="token string">&quot;column&quot;</span><span class="token punctuation">,</span> target <span class="token operator">=</span> <span class="token string">&quot;columnName&quot;</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>source <span class="token operator">=</span> <span class="token string">&quot;author&quot;</span><span class="token punctuation">,</span> target <span class="token operator">=</span> <span class="token string">&quot;userId&quot;</span><span class="token punctuation">)</span>
    <span class="token comment">// Long 转 Date</span>
    <span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>target <span class="token operator">=</span> <span class="token string">&quot;freeStartTime&quot;</span><span class="token punctuation">,</span> expression <span class="token operator">=</span> <span class="token string">&quot;java(new java.util.Date(req.getFreeStartTime()))&quot;</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>target <span class="token operator">=</span> <span class="token string">&quot;freeEndTime&quot;</span><span class="token punctuation">,</span> expression <span class="token operator">=</span> <span class="token string">&quot;java(new java.util.Date(req.getFreeEndTime()))&quot;</span><span class="token punctuation">)</span>
    <span class="token class-name">ColumnInfoDO</span> <span class="token function">toDo</span><span class="token punctuation">(</span><span class="token class-name">ColumnReq</span> req<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><p>这段代码定义了如何在 ColumnInfoDO 和 ColumnDTO 之间转换，以及如何从 ColumnReq 转换到 ColumnInfoDO。我将为你分析其每一部分：</p> <ol><li><strong>infotoDto 方法</strong></li></ol> <ul><li>使用 @Mapping 注解来指定属性映射规则。例如，将 ColumnInfoDO 的 id 属性映射到 ColumnDTO 的 columnId 属性。</li> <li>使用 expression 属性来定义更复杂的属性转换，例如从 Date 对象获取时间戳。</li></ul> <ol start="2"><li><strong>infoToDtos 方法</strong></li></ol> <p>显示了 MapStruct 如何轻松地转换对象列表。此方法将 List&lt;ColumnInfoDO&gt; 转换为 List&lt;ColumnDTO&gt;。因为单个对象的映射已经在 infotoDto 方法中定义，所以这里不需要额外的注解。</p> <ol start="3"><li><strong>toDo 方法</strong></li></ol> <p>对于 freeStartTime 和 freeEndTime，因为它们在 ColumnReq 中是 Long 类型的时间戳，而在ColumnInfoDO 中是 Date 类型，所以使用了 expression 属性进行转换。</p> <h3 id="mapping-注解"><a href="#mapping-注解" class="header-anchor">#</a> <a href="https://www.yuque.com/Mapping" target="_blank" rel="noopener noreferrer">@Mapping<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 注解</h3> <p>这里重点说一下 @Mapping 注解，当两个对象中的字段名或者字段类型不一致的时候，就需要该注解来进行转换。</p> <p>换句话说，如果对象的字段名/类型完全一样，就完全不需要该注解，Mapstruct 会自动拷贝。</p> <p>比如说 SimpleSource 和 SimpleDestination 的字段名和类型完全一样：</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SimpleSource</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> description<span class="token punctuation">;</span>
    <span class="token comment">// getters and setters</span>
<span class="token punctuation">}</span>
 
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SimpleDestination</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> description<span class="token punctuation">;</span>
    <span class="token comment">// getters and setters</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>就只需要定义映射器 SimpleSourceDestinationMapper 就行了。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapper</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">SimpleSourceDestinationMapper</span> <span class="token punctuation">{</span>
    <span class="token class-name">SimpleSourceDestinationMapper</span> <span class="token constant">INSTANCE</span> <span class="token operator">=</span> <span class="token class-name">Mappers</span><span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span><span class="token class-name">SimpleSourceDestinationMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">SimpleDestination</span> <span class="token function">sourceToDestination</span><span class="token punctuation">(</span><span class="token class-name">SimpleSource</span> source<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">SimpleSource</span> <span class="token function">destinationToSource</span><span class="token punctuation">(</span><span class="token class-name">SimpleDestination</span> destination<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>用的时候，可以这样 SimpleSourceDestinationMapper.INSTANCE 进行转换。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@RunWith</span><span class="token punctuation">(</span><span class="token class-name">SpringJUnit4ClassRunner</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@SpringBootTest</span><span class="token punctuation">(</span>classes <span class="token operator">=</span> <span class="token class-name">QuickForumApplication</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SimpleSourceDestinationMapperIntegrationTest</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Test</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">givenSourceToDestination_whenMaps_thenCorrect</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">SimpleSource</span> simpleSource <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SimpleSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        simpleSource<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">&quot;沉默王二&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        simpleSource<span class="token punctuation">.</span><span class="token function">setDescription</span><span class="token punctuation">(</span><span class="token string">&quot;是条狗&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">SimpleDestination</span> destination <span class="token operator">=</span> <span class="token class-name">SimpleSourceDestinationMapper</span><span class="token punctuation">.</span><span class="token constant">INSTANCE</span><span class="token punctuation">.</span><span class="token function">sourceToDestination</span><span class="token punctuation">(</span>simpleSource<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">assertEquals</span><span class="token punctuation">(</span>simpleSource<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> destination<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">assertEquals</span><span class="token punctuation">(</span>simpleSource<span class="token punctuation">.</span><span class="token function">getDescription</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> destination<span class="token punctuation">.</span><span class="token function">getDescription</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>OK，回到 <a href="https://www.yuque.com/Mapping" target="_blank" rel="noopener noreferrer">@Mapping<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 注解。</p> <p>@Mapping 是 MapStruct 中用于定义字段之间映射规则的注解。它非常灵活，能够处理各种复杂的映射情况。以下是 @Mapping 的一些常见用法：</p> <ol><li><strong>基本映射</strong>：</li></ol> <p>通过指定 source 和 target 来映射源对象的属性到目标对象的属性。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>source <span class="token operator">=</span> <span class="token string">&quot;name&quot;</span><span class="token punctuation">,</span> target <span class="token operator">=</span> <span class="token string">&quot;fullName&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这将源对象的 name 属性映射到目标对象的 fullName 属性。</p> <ol start="2"><li><strong>常量映射</strong></li></ol> <p>可以将目标字段设置为固定的常量值。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>target <span class="token operator">=</span> <span class="token string">&quot;status&quot;</span><span class="token punctuation">,</span> constant <span class="token operator">=</span> <span class="token string">&quot;ACTIVE&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这会将目标对象的 status 属性设置为 &quot;ACTIVE&quot;。</p> <ol start="3"><li><strong>默认值</strong></li></ol> <p>当源属性为 null 时，可以为目标属性设置默认值。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>source <span class="token operator">=</span> <span class="token string">&quot;count&quot;</span><span class="token punctuation">,</span> target <span class="token operator">=</span> <span class="token string">&quot;total&quot;</span><span class="token punctuation">,</span> defaultValue <span class="token operator">=</span> <span class="token string">&quot;0&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果 count 为 null，则 total 将被设置为 &quot;0&quot;。</p> <ol start="4"><li><strong>表达式</strong></li></ol> <p>对于需要更复杂的转换逻辑，可以使用 Java 表达式。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>target <span class="token operator">=</span> <span class="token string">&quot;timestamp&quot;</span><span class="token punctuation">,</span> expression <span class="token operator">=</span> <span class="token string">&quot;java(source.getDate().getTime())&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ol start="5"><li><strong>日期格式</strong></li></ol> <p>对于日期和字符串之间的映射，可以指定日期格式。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>source <span class="token operator">=</span> <span class="token string">&quot;date&quot;</span><span class="token punctuation">,</span> target <span class="token operator">=</span> <span class="token string">&quot;formattedDate&quot;</span><span class="token punctuation">,</span> dateFormat <span class="token operator">=</span> <span class="token string">&quot;yyyy-MM-dd&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这会将 Date 对象转换为 &quot;yyyy-MM-dd&quot; 格式的字符串。</p> <ol start="6"><li><strong>条件映射</strong></li></ol> <p>使用 qualifiedByName 或 qualifiedBy 来指定一个条件方法或注解，这些方法/注解决定是否应该进行映射。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>source <span class="token operator">=</span> <span class="token string">&quot;value&quot;</span><span class="token punctuation">,</span> target <span class="token operator">=</span> <span class="token string">&quot;data&quot;</span><span class="token punctuation">,</span> qualifiedByName <span class="token operator">=</span> <span class="token string">&quot;specialConverter&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这里，映射会使用名为 specialConverter 的方法进行。</p> <ol start="7"><li><strong>嵌套映射</strong></li></ol> <p>当处理嵌套对象时，可以使用点表示法。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>source <span class="token operator">=</span> <span class="token string">&quot;address.street&quot;</span><span class="token punctuation">,</span> target <span class="token operator">=</span> <span class="token string">&quot;streetName&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这会将源对象中的 address 对象的 street 属性映射到目标对象的 streetName 属性。</p> <ol start="8"><li><strong>忽略映射</strong></li></ol> <p>在某些情况下，可能不希望特定的属性被映射，可以使用 ignore。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>target <span class="token operator">=</span> <span class="token string">&quot;internalId&quot;</span><span class="token punctuation">,</span> ignore <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这会确保目标对象的 internalId 属性不被设置。</p> <ol start="9"><li><strong>使用自定义映射方法</strong></li></ol> <p>可以指定自定义方法来进行映射。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapping</span><span class="token punctuation">(</span>target <span class="token operator">=</span> <span class="token string">&quot;data&quot;</span><span class="token punctuation">,</span> source <span class="token operator">=</span> <span class="token string">&quot;value&quot;</span><span class="token punctuation">,</span> qualifiedByName <span class="token operator">=</span> <span class="token string">&quot;customMethod&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="spring-依赖注入"><a href="#spring-依赖注入" class="header-anchor">#</a> Spring 依赖注入</h3> <p>截止到目前为止，我们一直在使用 Mappers.getMapper 来获取映射器 INSTANCE。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">ColumnStructMapper</span> <span class="token constant">INSTANCE</span> <span class="token operator">=</span> <span class="token class-name">Mappers</span><span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span> <span class="token class-name">ColumnStructMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果是在 Spring 环境下，还可以在 <a href="https://www.yuque.com/Mapper" target="_blank" rel="noopener noreferrer">@Mapper<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 注解中添加 componentModel = &quot;spring&quot; 参数来告诉 MapStruct 在生成映射实现类的时候，提供 Spring 依赖注入。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapper</span><span class="token punctuation">(</span>componentModel <span class="token operator">=</span> <span class="token string">&quot;spring&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ColumnStructMapper</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>这样我们在使用映射器的时候，可以直接通过 @Autowired 注解来注入 ColumnStructMapper 对象，然后就可以直接这样使用。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> <span class="token class-name">ColumnStructMapper</span> columnStructMapper<span class="token punctuation">;</span>
<span class="token class-name">ColumnInfoDO</span> columnInfoDO <span class="token operator">=</span> columnStructMapper<span class="token punctuation">.</span><span class="token function">toDo</span><span class="token punctuation">(</span>req<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>这样就不需要在映射器接口中添加 INSTANCE 了。</p> <h2 id="mapstruct-插件"><a href="#mapstruct-插件" class="header-anchor">#</a> MapStruct 插件</h2> <p>如果你在 Intellij IDEA 中安装 MapStruct 插件的话，直接在插件市场搜 MapStruct 关键字就可以了。</p> <p><img alt="" data-src="https://pic.yupi.icu/5563/202311081048480.png" loading="lazy" class="lazy"></p> <p>安装完成后，可以直接在 <a href="https://www.yuque.com/Mapper" target="_blank" rel="noopener noreferrer">@Mapper<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 接口和它的实现类之间快速导航。</p> <p><img alt="" data-src="https://pic.yupi.icu/5563/202311081048118.png" loading="lazy" class="lazy"></p> <p>比如说点击上图中的跳转小图标，就可以直接到实现类。</p> <p><img alt="" data-src="https://pic.yupi.icu/5563/202311081048973.png" loading="lazy" class="lazy"></p> <h2 id="mapstruct-的背后原理"><a href="#mapstruct-的背后原理" class="header-anchor">#</a> <strong>MapStruct 的背后原理</strong></h2> <p>Java 程序执行的过程，是由编译器先把 java 文件编译成 class 字节码文件，然后由 JVM 去解释执行 class 文件。</p> <p>Mapstruct 正是在 java 文件到 class 这一步帮我们实现了转换方法，即做了预处理，提前编译好文件，用过 lombok 的小伙伴一定能理解其好处。</p> <p>首先我们来看最简单的 SimpleSourceDestinationMapper，它是这样定义的。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Mapper</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">SimpleSourceDestinationMapper</span> <span class="token punctuation">{</span>
    <span class="token class-name">SimpleSourceDestinationMapper</span> <span class="token constant">INSTANCE</span> <span class="token operator">=</span> <span class="token class-name">Mappers</span><span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span><span class="token class-name">SimpleSourceDestinationMapper</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token class-name">SimpleDestination</span> <span class="token function">sourceToDestination</span><span class="token punctuation">(</span><span class="token class-name">SimpleSource</span> source<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token class-name">SimpleSource</span> <span class="token function">destinationToSource</span><span class="token punctuation">(</span><span class="token class-name">SimpleDestination</span> destination<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><br>其在编译后会生成两个文件 SimpleSourceDestinationMapper 和 SimpleSourceDestinationMapperImpl。</p> <p><img alt="" data-src="https://pic.yupi.icu/5563/202311081049390.png" loading="lazy" class="lazy"></p> <p>通过终端可以看到 class 文件的后缀。</p> <p><img alt="" data-src="https://pic.yupi.icu/5563/202311081049134.png" loading="lazy" class="lazy"></p> <p>OK，我们直接来看 class 文件的内容，当然是反编译后的，Intellij IDEA 可以直接打开，先是 SimpleSourceDestinationMapper。</p> <p><img alt="" data-src="https://pic.yupi.icu/5563/202311081049183.png" loading="lazy" class="lazy"></p> <p>再来看实现类 SimpleSourceDestinationMapperImpl，我们直接贴反编译后的代码。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SimpleSourceDestinationMapperImpl</span> <span class="token keyword">implements</span> <span class="token class-name">SimpleSourceDestinationMapper</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token class-name">SimpleSourceDestinationMapperImpl</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">SimpleDestination</span> <span class="token function">sourceToDestination</span><span class="token punctuation">(</span><span class="token class-name">SimpleSource</span> source<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>source <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token class-name">SimpleDestination</span> simpleDestination <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SimpleDestination</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            simpleDestination<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span>source<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            simpleDestination<span class="token punctuation">.</span><span class="token function">setDescription</span><span class="token punctuation">(</span>source<span class="token punctuation">.</span><span class="token function">getDescription</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> simpleDestination<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token class-name">SimpleSource</span> <span class="token function">destinationToSource</span><span class="token punctuation">(</span><span class="token class-name">SimpleDestination</span> destination<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>destination <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token class-name">SimpleSource</span> simpleSource <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SimpleSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            simpleSource<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span>destination<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            simpleSource<span class="token punctuation">.</span><span class="token function">setDescription</span><span class="token punctuation">(</span>destination<span class="token punctuation">.</span><span class="token function">getDescription</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> simpleSource<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><p>其实内容和我们直接去写 Converter 是一样的，通过 new 创建一个对象，然后通过 set 方法进行赋值。</p> <p>如果是 @Mapper(componentModel = &quot;spring&quot;) 的话，在生成的时候，会带上 <a href="https://www.yuque.com/Component" target="_blank" rel="noopener noreferrer">@Component<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 注解。 <br><img alt="" data-src="https://pic.yupi.icu/5563/202311081050327.png" loading="lazy" class="lazy"></p> <blockquote><p>使用 <a href="https://www.yuque.com/Component" target="_blank" rel="noopener noreferrer"><strong>@Component</strong><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 注解的类将会在 Spring 的组件扫描期间被检测并注册到 ApplicationContext 中，从而使其成为一个 Spring Bean。</p></blockquote> <p>这也是为什么我们可以直接通过 <a href="https://www.yuque.com/Autowired" target="_blank" rel="noopener noreferrer">@Autowired<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 注解获得映射器对象的原因。</p> <p>由于 MapStruct 并不是在运行时使用 Java 反射来实现对象之间的映射，而是在编译时生成明确、简单、易于跟踪的普通 Java 代码。这意味着它的执行速度更快，因为没有运行时的反射开销，并且可以避免与反射相关的各种问题。</p></div> <footer class="page-edit" style="margin:0;"><div class="edit-link"><a href="https://github.com/liyupi/codefather/edit/master/知识碎片/Bean 拷贝之 MapStruct.md" target="_blank" rel="noopener noreferrer">完善页面</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">最近更新:</span> <span class="time">11/11/2023, 8:17:47 PM</span></div></footer> <!----></div> <div class="toc-container-sidebar"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="max-height:86vh"><div style="font-weight:bold;">Bean 拷贝之 MapStruct</div> <hr> <div class="toc-box"><ul class="toc-sidebar-links"><li><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#传统写法带来的困扰" class="toc-sidebar-link">传统写法带来的困扰</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#mapstruct-的用法" class="toc-sidebar-link">MapStruct 的用法</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#引入依赖" class="toc-sidebar-link">引入依赖</a></li><li class="toc-sidebar-sub-header"><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#定义映射器接口" class="toc-sidebar-link">定义映射器接口</a></li><li class="toc-sidebar-sub-header"><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#mapping-注解" class="toc-sidebar-link">@Mapping 注解</a></li><li class="toc-sidebar-sub-header"><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#spring-依赖注入" class="toc-sidebar-link">Spring 依赖注入</a></li></ul></li><li><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#mapstruct-插件" class="toc-sidebar-link">MapStruct 插件</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/bean-%E6%8B%B7%E8%B4%9D%E4%B9%8B-mapstruct/#mapstruct-的背后原理" class="toc-sidebar-link">MapStruct 的背后原理</a><ul class="toc-sidebar-sub-headers"></ul></li></ul></div></div></div></div></div>  <main class="footer"> <div class="copy-right"><span class="name"> 编程导航   |     </span> <a target="_blank" rel="noreferrer">
          
        </a></div></main></main> <aside class="page-sidebar"> <div class="page-side-toolbar"></div>  </aside></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.12ab4756.js" defer></script><script src="/assets/js/2.fd06b53e.js" defer></script><script src="/assets/js/48.48f286ba.js" defer></script>
  </body>
</html>
